package com.imooc.distributedemo.resume.thread;

import java.util.concurrent.Phaser;

public class CalculationTask implements Runnable {

    private final int phase;
    private final Phaser phaser;

    public CalculationTask(int phase, Phaser phaser) {
        this.phase = phase;
        this.phaser = phaser;
    }

    @Override
    public void run() {
        System.out.println("CalculationTask " + Thread.currentThread().getName() + " starts phase " + phase);
        // 根据阶段号执行不同的计算逻辑
        switch (phase) {
            case 0:
                // 第一阶段的计算逻辑
                // ...
                System.out.println("standard1");
                break;
            case 1:
                // 第二阶段的计算逻辑
                // ...
                break;
            case 2:
                // 第三阶段的计算逻辑
                // ...
                System.out.println("standard2");
                break;
            // ...
            default:
                System.out.println("standard3");
                break;
        }
        System.out.println("CalculationTask " + Thread.currentThread().getName() + " finishes phase " + phase);
        phaser.arriveAndAwaitAdvance(); // 等待其他线程到达
    }
}
